(function (angular) {
    'use strict';

    angular.module('ytong.controllers').controller('customersCtrl',customersCtrl);

    function customersCtrl(customerManager, ModalService, Constants){

        this.customers = customerManager.getCustomers();

        this.editCustomer = function(customer){
            ModalService.showModal({
                templateUrl: 'editCustomer',
                controller: 'editCustomerControl as ecc',
                inputs:{
                    customer: customer
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.ADD){
                            customerManager.addCustomer(result.customer);
                        }else if(result.action === Constants.COMMANDS.UPDATE){
                            customerManager.updateCustomer(result.customer);
                        }else if(result.action === Constants.COMMANDS.DELETE){
                            customerManager.deleteCustomer(result.customer);
                        }
                    });
                });
        };
    };


    angular.module('ytong.controllers').controller('editCustomerControl', editCustomerControl);

    function editCustomerControl(close, customer){

        this.customer = customer;

        this.isDeleteAllowed = function(){
            return (customer !== undefined);
        };

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.customer = this.customer;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };


}(angular));